package com.browsesoft.htmlcomponent;

import java.util.LinkedList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.ExtendElement;
import com.browsesoft.dbtools.DBTools;

public class HTMLDBTableShowModel extends HTMLDBTableModel {
	/**
	 * 得到列数
	 * 
	 * @return 列数
	 */
	public int getColumnCount() {
		return cnames.size() + 1;
	}

	/**
	 * 得到某列的操作类型
	 * 
	 * @return 类型
	 */
	public String getType(int col) {
		if (col < cnames.size()) {
			return "td";
		} else if (col == cnames.size()) {
			return "detail";
		}
		return null;
	}

	public void modifyCellValue(Element td, Document doc, int row, int col) {
		try {
			// 显示其他
			if (col < cnames.size()) {
				// 得到链接
				LinkedList nl = ExtendElement.findElements(td, "type", "link");
				if (nl.size() != 0) {
					Element link = (Element) nl.get(0);
					String value = link.getAttribute("onclick");
					int index = DBTools.indexOf(datas[0], "id");
					value = value.replaceAll("point=", "point="
							+ datas[row + 1][index]);
					link.setAttribute("onclick", value);
				}
				int index = DBTools.indexOf(datas[0], (String) enames.get(col));
				if (index != -1) {
					ExtendElement.replaceElementContext(td, doc
							.createTextNode(datas[row + 1][index]));
				} else {
					ExtendElement.replaceElementContext(td, doc
							.createTextNode(""));
				}
			}
			// 详细信息
			else if (col == cnames.size()) {
				Element e = (Element) ExtendElement.findElements(td, "type",
						"link").get(0);
				String str = e.getAttribute("onclick");
				int index = DBTools.indexOf(datas[0], "id");
				str = str.replaceAll("id=", "id=" + datas[row + 1][index]);
				str = str.replaceAll("templatename=", "templatename=" + form);
				e.setAttribute("onclick", str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}